﻿using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

namespace ExcelExporter.Runtime
{
    public class DefaultLoader : IDataLoader
    {
        public async void LoadConfig(string path, loadConfigCallBack callBack)
        {
            UnityWebRequest request = UnityWebRequest.Get(path);
            while (!request.isDone)
            {
                if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
                {
                    callBack.FailureCallBack?.Invoke();
                }
                await Task.Delay(10);
            }
            if (request.downloadHandler.data == null)
            {
                callBack.FailureCallBack?.Invoke();
            }
            callBack.SuccessCallBack?.Invoke(request.downloadHandler.data);
        }
    }
}